package com.windows;

import com.entity.Message;
import com.util.GoodUtil;
import com.util.LogUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SellGood extends JDialog {

    private String name;
    private int amount;

    public SellGood(String table,String name,int index) {

        this.name = name;

        //窗口初始化设置
        setSize(400, 100);
        setLocationRelativeTo(null);
        setResizable(false);                                   //此窗口大小不能改变
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("超市管理系统-------销售");
        Container c = getContentPane();
        setLayout(new GridLayout(2,1));
        setModal(true);



        JPanel jp1 = new JPanel();
        JLabel lb = new JLabel("商品数量");
        lb.setFont(new Font("宋体",Font.PLAIN,20));
        JTextArea text = new JTextArea();
        text.setFont(new Font("宋体",Font.PLAIN,20));
        text.setColumns(17);
        jp1.add(lb);
        jp1.add(text);

        JButton store = new JButton("确定");
        JButton quit = new JButton("返回");

        JPanel jp2 = new JPanel();
        jp2.add(store);
        jp2.add(quit);


        store.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                amount = index - Integer.valueOf(text.getText());
                if(amount < 0) {

                    JOptionPane.showMessageDialog(null, "产品不足", "出售结果",JOptionPane.PLAIN_MESSAGE);

                }else {

                    Message message = GoodUtil.sellGood(name,amount);


                    JOptionPane.showMessageDialog(null, message.getMessage(), "出售结果",JOptionPane.PLAIN_MESSAGE);
                    if(message.getFlag()) {
                        LogUtil.addlog("售卖"+name+"-->"+text.getText()+"成功");
                    }else {
                        LogUtil.addlog("售卖"+name+"-->"+text.getText()+"失败");
                    }
                    SellGood.this.dispose();

                }

            }
        });

        quit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成的方法存根

                SellGood.this.dispose();
            }
        });

        c.add(jp1);
        c.add(jp2);
        setVisible(true);



    }


}
